﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using Channelvn.Cached.Common;
using EntityFramework;

namespace ProjectQuaDep.Controls
{
    public partial class DetailProduct__ : System.Web.UI.UserControl
    {
        protected string urlZone = "";
        protected string titleZone = "";
        int PageIndex = lib.PageIndex;
        int PageSize = 10;
        private string strHead = "<a href=\"{0}\">{1}</a>";
        private int IMG_WIDTH = 480, IMG_HEIGHT = 475;
        private string sFormatNews = @"<li><a href=""{0}""><img src=""{1}"" /></a><h3><a href=""{0}"">{2}</a></h3><p>{3}</p></li>";
        protected long productId = 0;
        private string shortUrl = string.Empty;
        protected string linkFB = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            int zoneID = BO.lib.ZoneID;
            productId = Lib.QueryString.ProductID;
            shortUrl = Lib.QueryString.shortURL;

            if (productId > 0)
            {
                Product pd = ProductsBO.GetProductDetail(productId);

                //add coookie
                string cookieName = "_GaoRang_Product_" + productId.ToString();
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

                if (cookie == null)
                {
                    try
                    {
                        int countVisit = ProductsBO.GetCountVisit(productId);
                        if (countVisit > 0)
                        {
                            cookie = new HttpCookie(cookieName);
                            cookie.Values["ProductID"] = productId.ToString();
                            cookie.Expires = DateTime.Now.AddHours(24);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            ltrCounter.Text = countVisit.ToString();
                        }

                    }
                    catch
                    {
                    }
                }
                //

                ltrCategory.Text = string.Format(strHead,
                                                        "/" + shortUrl + ".htm",
                                                        ZoneProductBO.GetZoneProductNameByShortUrl(shortUrl));

                linkFB = ConfigurationManager.AppSettings["Domain"] + pd.Url;

                if (pd.Avatar.Contains(","))
                {
                    string[] arr = pd.Avatar.Split(',');
                    if (arr.Length > 0)
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] != "")
                            {
                                ltrMainImg.Text += "<img src=\"" + BO.Utils.CropImage(IMG_WIDTH, IMG_HEIGHT, arr[i]) + "\" data-zoom-image=\"" + BO.Utils.CropImage(IMG_WIDTH, IMG_HEIGHT, arr[i]) + "\"/>";
                            }
                            if (i == 3)
                                break;
                        }
                    }
                }

                ltrTitle.Text = pd.Title;
                ltrCode.Text = pd.id.ToString();
                ltrSapo.Text = pd.Sapo;
                ltrPrice.Text = pd.TypeSale == 1 ? "Giá: Liên hệ" : BO.Utils.ConvertMoneyNice(pd.price.ToString(), BO.Const.FormatMoney);
                ltrContent.Text = pd.longContent;

                if (pd.NewsRelation != null && pd.NewsRelation != "")
                {
                    rptData.DataSource = ProductsBO.GetRelationProduct(pd.NewsRelation.ToString());
                    rptData.DataBind();
                }
                else
                {
                    pnSPLienQuan.Visible = false;
                }

                //set meta
                string Trang = "";
                if (PageIndex > 1)
                {
                    Trang = " | Trang " + PageIndex;
                }
                Page.Title = pd.Title + Trang;
                string hDesc = pd.Title + Trang;
                string hKey = pd.Title;
                BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
                BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
                BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
            }
        }
    }
}